package com.xgpro.mobile.studycentre.controller;

import com.xgpro.common.controller.BaseController;
import com.xgpro.common.enums.ResultCodeEnum;
import com.xgpro.common.exceptions.BusinessRuntimeException;
import com.xgpro.common.results.ResponseResult;
import com.xgpro.domain.studycentre.vo.EVAFront;
import com.xgpro.domain.studycentre.vo.Evaluation;
import com.xgpro.domain.studycentre.vo.Relationship;
import com.xgpro.domain.studycentre.vo.TeaStuId;
import com.xgpro.mobile.studycentre.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author tx
 * @version 1.0
 * @date 2019/8/28 21:58
 */
@RestController
@RequestMapping("/teacher")
@CrossOrigin(allowCredentials="true")
public class TeacherController extends BaseController {

    @CrossOrigin(allowCredentials="true")
    @RequestMapping("/test")
    public void setSession(HttpSession httpSession){
        httpSession.setAttribute("id",260);
        httpSession.setAttribute("deptId",1);
        List<Integer> roleIds= new ArrayList<>();
        roleIds.add(1);
        roleIds.add(2);
        roleIds.add(3);
        httpSession.setAttribute("roleIds",roleIds);
    }

    /**
     * 老师解除关系的状态
     */
    private static final Integer TEA_CANCLE_STATUS=1;

    /**
     *老师评价标志
     */
    private static  final Integer TEA_EVAKIND=0;

    @Autowired
    private TeacherService teacherService;

    /**
     * 查询导师的学生信息
     * @param
     * @return
     */
    @RequestMapping("/qryStudentsByTeacherId")
    public ResponseResult qryStudentsByTeacherId(HttpSession session){
        try {
            Integer id = (Integer) session.getAttribute("id");
            if (id == null) {
                throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(), ResultCodeEnum.NO_SESSION.getMessage());
            }
            return ResponseResult.ok(teacherService.qryStudentsByTeacherId(id));
            }catch (Exception e){
            //e.printStackTrace();
            return ResponseResult.failure(ResultCodeEnum.NO_SESSION.getCode(),ResultCodeEnum.NO_SESSION.getMessage());
            }
    }


    /**
     * 查询个人信息
     * @param id
     * @return
     */
    @RequestMapping("/qryUserInfoById")
    public ResponseResult qryUserInfoById(Integer id){
        return ResponseResult.ok(teacherService.qryUserInfoById(id));
    }

    /**
     * 解除师生关系
     * @param session
     * @param relationship
     * @return
     */
    @RequestMapping("/cancelRelationship")
    public ResponseResult cancelRelationship(HttpSession session,@RequestBody Relationship relationship){
        try{
            Integer id=(Integer)session.getAttribute("id");
            if(id==null){
                throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(),ResultCodeEnum.NO_SESSION.getMessage());
            }
            //老师解除关系的状态
            if(TEA_CANCLE_STATUS.equals(relationship.getCancleFlag())){
                relationship.setTeaId(id);
            }else{
                relationship.setStuId(id);
            }
            return  ResponseResult.ok(teacherService.cancelRelationship(relationship));
            }catch (BusinessRuntimeException e){
                //e.printStackTrace();
                return ResponseResult.failure(ResultCodeEnum.NO_SESSION.getCode(),ResultCodeEnum.NO_SESSION.getMessage());
            }
            catch (Exception e){
                //e.printStackTrace();
                return  ResponseResult.failure(ResultCodeEnum.CANCLE_FALE.getCode(),ResultCodeEnum.CANCLE_FALE.getMessage());
            }
    }

  /**
	 * 确认解除关系
	 * 
	 * @param session
	 * @param teaStuId
	 * @return
	 */
	@RequestMapping("/sureCancle")
	public ResponseResult sureCancle(HttpSession session, @RequestBody TeaStuId teaStuId) {
		try {
			Integer id = (Integer) session.getAttribute("id");
			if (id == null) {
				throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(),
						ResultCodeEnum.NO_SESSION.getMessage());
			}
			// 老师解除关系的状态
			if (teaStuId.getStuId() != null && !"".equals(teaStuId.getStuId())) {
				return ResponseResult.ok(teacherService.sureCancle(id, teaStuId.getStuId()));
			} else {
				return ResponseResult.ok(teacherService.sureCancle(teaStuId.getTeaId(), id));
			}
		} catch (BusinessRuntimeException e) {
			//e.printStackTrace();
			return ResponseResult.failure(e.getErrorCode(), e.getMessage());
		}
	}

    /**
	 * 取消解除关系
	 * 
	 * @param session
	 * @param teaStuId
	 * @return
	 */
	@RequestMapping("/noSureCancle")
	public ResponseResult noSureCancle(HttpSession session, @RequestBody TeaStuId teaStuId) {
		try {
			Integer id = (Integer) session.getAttribute("id");
			if (id == null) {
				throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(),
						ResultCodeEnum.NO_SESSION.getMessage());
			}
			// 老师取消解除关系的状态
			if (teaStuId.getStuId() != null && !"".equals(teaStuId.getStuId())) {
				return ResponseResult.ok(teacherService.noSureCancle(id, teaStuId.getStuId()));
			} else {
				return ResponseResult.ok(teacherService.noSureCancle(teaStuId.getTeaId(), id));
			}
		} catch (BusinessRuntimeException e) {
			//e.printStackTrace();
			return ResponseResult.failure(e.getErrorCode(), e.getMessage());
		}
	}


    /**
     * 评价
     * @param session
     * @param evaluation
     * @return
     */
    @RequestMapping("/evaluation")
    public ResponseResult evaluation(HttpSession session, @RequestBody Evaluation evaluation){
        try {
            Integer id = (Integer) session.getAttribute("id");
            if (id == null) {
                throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(), ResultCodeEnum.NO_SESSION.getMessage());
            }
            //老师评价
            if (TEA_EVAKIND.equals(evaluation.getEvaKind())) {
                evaluation.setTeaId(id);
            } else {
                evaluation.setStuId(id);
            }
            return ResponseResult.ok(teacherService.evaluation(evaluation));
        }catch (BusinessRuntimeException e){
            //e.printStackTrace();
            return ResponseResult.failure(ResultCodeEnum.NO_SESSION.getCode(),ResultCodeEnum.NO_SESSION.getMessage());
        }catch (Exception e){
            //e.printStackTrace();
            return ResponseResult.failure(ResultCodeEnum.EVA_FALE.getCode(),ResultCodeEnum.EVA_FALE.getMessage());
        }
    }

    /**
     * 查询导师的学生信息
     * @param
     * @return
     */
    @RequestMapping("/qryTeacherByStudentId")
    public ResponseResult qryTeacherByStudentId(HttpSession session){
        try {
            Integer id = (Integer) session.getAttribute("id");
            if (id == null) {
                throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(), ResultCodeEnum.NO_SESSION.getMessage());
            }
            return ResponseResult.ok(teacherService.qryTeacherByStudentId(id));
        }catch (Exception e){
            //e.printStackTrace();
            return ResponseResult.failure(ResultCodeEnum.NO_SESSION.getCode(),ResultCodeEnum.NO_SESSION.getMessage());
        }
    }

    /**
     * 显示带徒导师列表
     * @return
     */
    @RequestMapping("/qryTeaSimple")
    public ResponseResult qryTeaSimple(String name){
        return ResponseResult.ok(teacherService.qryTeaSimple(name));
    }

    /**
     * 申请导师
     * @param httpSession
     * @param teaId
     * @return
     */
    @RequestMapping("/applayTea")
    public ResponseResult applayTea(HttpSession httpSession,Integer teaId) throws Exception {
        try {
            Integer userId = (Integer) httpSession.getAttribute("id");
            if (userId == null) {
                throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(), ResultCodeEnum.NO_SESSION.getMessage());
            }
            Integer result = teacherService.applayTea(userId, teaId);
            if(result.equals(0)){
                throw new BusinessRuntimeException(ResultCodeEnum.ARRNUM_FALE.getCode(),ResultCodeEnum.ARRNUM_FALE.getMessage());
            }
            return ResponseResult.ok(result);
        }catch (BusinessRuntimeException e){
            //e.printStackTrace();
            return ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }
    }

    /**
     * 根据状态查询徒弟信息
     * @param httpSession
     * @param status
     * @return
     */
    @RequestMapping("/qryStuSimpleByStatus")
    public ResponseResult qryStuSimpleByStatus(HttpSession httpSession,Integer status){
        try {
            Integer id = (Integer) httpSession.getAttribute("id");
            if (id == null) {
                throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(), ResultCodeEnum.NO_SESSION.getMessage());
            }
            return ResponseResult.ok(teacherService.qryStuSimpleByStatus(id,status));
        }catch (BusinessRuntimeException e){
            //e.printStackTrace();
            return ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }
    }

    /**
     * 处理拜师申请
     * @param httpSession
     * @param stuId
     * @param status
     * @return
     */
    @RequestMapping("/dealApplay")
    public ResponseResult dealApplay(HttpSession httpSession,Integer stuId,Integer status){
        try {
            Integer id = (Integer) httpSession.getAttribute("id");
            if (id == null) {
                throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(), ResultCodeEnum.NO_SESSION.getMessage());
            }
            return ResponseResult.ok(teacherService.dealApplay(stuId,status,id));
        }catch (BusinessRuntimeException e){
            //e.printStackTrace();
            return ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }
    }

    /**
     * 解除关系学生数
     * @param session
     * @return
     */
    @RequestMapping("/countStudents")
    public ResponseResult countStudents(HttpSession session){
        try {
            Integer id = (Integer) session.getAttribute("id");
            if (id == null) {
                throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(), ResultCodeEnum.NO_SESSION.getMessage());
            }
            return ResponseResult.ok(teacherService.countStudents(id));
        }catch (BusinessRuntimeException e){
            //e.printStackTrace();
            return ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }
    }

    /**
     * 老师解除关系
     * @param session
     * @return
     */
    @RequestMapping("/countTeacher")
    public ResponseResult countTeacher(HttpSession session){
        try {
            Integer id = (Integer) session.getAttribute("id");
            if (id == null) {
                throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(), ResultCodeEnum.NO_SESSION.getMessage());
            }
            return ResponseResult.ok(teacherService.countTeacher(id));
        }catch (BusinessRuntimeException e){
            //e.printStackTrace();
            return ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }
    }


    /**
     * 学生评价表
     * @param httpSession
     * @param map
     * @return
     */
    @RequestMapping("/stuEva")
    public ResponseResult stuEva(HttpSession httpSession,@RequestBody Map map){
        try{
            Integer id =(Integer) httpSession.getAttribute("id");
            if(id==null||"".equals(id)){
                throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(),ResultCodeEnum.NO_SESSION.getMessage());
            }
            return ResponseResult.ok(teacherService.stuEva(id,map));
        }catch (BusinessRuntimeException e){
            //e.printStackTrace();
            return ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }
    }

    /**
     * 学生评价表
     * @param httpSession
     * @param map
     * @return
     */
    @RequestMapping("/teaEva")
    public ResponseResult teaEva(HttpSession httpSession,@RequestBody Map map){
        try{
            Integer id =(Integer) httpSession.getAttribute("id");
            if(id==null||"".equals(id)){
                throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(),ResultCodeEnum.NO_SESSION.getMessage());
            }
            return ResponseResult.ok(teacherService.teaEva(id,map));
        }catch (BusinessRuntimeException e){
            //e.printStackTrace();
            return ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }
    }

    /**
     * 导师学生评估
     * @param evaFront
     * @return
     */
    @RequestMapping("/getEVAResult")
    public ResponseResult getEVAResult(@RequestBody EVAFront evaFront){
        return ResponseResult.ok(teacherService.getEVAResult(evaFront));
    }
}
